﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ChemicalEquilibrium
{
    /// <summary>
    /// 物质传递
    /// </summary>
    public class ReactantTransform
    {
        public List<Reactants> Reactants;

        public ReactantTransform(params Reactants[] Former)
        {
            Reactants = new List<Reactants>(Former);
        }

        public ReactantTransform(Reactants Latter, params Reactants[] Former)
        {
            Reactants = new List<Reactants>(Former);

            Reactants.Add(Latter);
        }

        public void Transform()
        {
            int sum = 0;
            for (int i = 0; i < this.Reactants.Count; i++)
            {
                sum += this.Reactants[i].Active.Count;
            }
            int avg = sum / this.Reactants.Count;

            for (int i = 0; i < this.Reactants.Count; i++)
            {
                int count = this.Reactants[i].Active.Count - avg;
                int length = Math.Abs(count);

                for (int j = 0; j < length; j++)
                {
                    if (count > 0)
                    {
                        if (this.Reactants[i].Active.Count == 0) break;

                        this.Reactants[i].Inactive.Enqueue(this.Reactants[i].Active.Dequeue());
                    }
                    else if (count < 0)
                    {
                        if (this.Reactants[i].Inactive.Count == 0) break;
                        this.Reactants[i].Active.Enqueue(this.Reactants[i].Inactive.Dequeue());
                    }
                }
            }
        }
    }
}
